﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    public partial class TKD052
    {
        #region ==CKUNDNR===========
        public static readonly DependencyProperty CKUNDNRProperty = DependencyProperty.Register("CKUNDNR",
            typeof(string), typeof(TKD052),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCKUNDNRChanged));
        public string CKUNDNR
        {
            get
            {
                return (string)GetValue(CKUNDNRProperty);
            }
            set
            {
                SetValue(CKUNDNRProperty, value);
                OnCKUNDNRChg(value);
            }
        }
        private static void OnCKUNDNRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TKD052;
            if (src != null)
            {
                src.CKUNDNR = (string)e.NewValue;
            }
        }
        #endregion
        public TKD052()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VKUNDADR();
            DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                //KDAADRSL = "";
            }
            else
            {
               // KDAADRSL = dwMaster["KDAADRSL"].ToString();
            }
        }

        public void OnCKUNDNRChg(string value)
        {
            if (value == "")
                return;
            ORDERNUM = value;
        }
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            KUNDNR = EXAUF;
            if (EXAUF.Length < 8)
                return;
            dwData = TBMain.NewRow();
            dwData["KDAMANDANT"] = app.Mandant;
            dwData["KDAKUNDNR"] = " AND KDAKUNDNR='" + EXAUF + "'";
            SResult = TMV.QueryKUNDADR(dwData, 1);
            MergeData(SResult, false);
            IsSaved = false;
            base.OnEXAUFChg(vlaue);
        }
        public override void OnKUNDNRChg(string value)
        {
            
            base.OnKUNDNRChg(value);

        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            dwData["KDAMANDANT"] = app.Mandant;
            dwData["KDANAME1"] = ART.Trim();
            dwData["KDASTRASSE1"] = FARBE.Trim();
            SResult = TMV.QueryKUNDADR(dwData, 5);
            MergeData(SResult, false, true);
             base.QueryCommand(sender, e);
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            base.SaveCommand(sender, e);
         }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if(dwMaster==null)
                return;
            TMV.DeleteKUNDADR(dwMaster, 1);
            filter = "KDAKUNDNR='" + dwMaster["KDAKUNDNR"].ToString() + "' AND KDAADRSL='" + dwMaster["KDAADRSL"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();  
            base.DeleteCommand(sender, e);
        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsNew)
                {
                    IsNew = true;
                    return;
                }
                if (KDAADRSL.Trim() == "TH")
                {
                    MessageBox.Show("请给出合适的地址编号TH0/TH1");
                    return;
                }
                PM["KUNDNR"] = KUNDNR;
                PM["KDAADRSL"] = ART.PadRight(8);
                TMV.NEWKDR(PM);
                base.NewCommand(sender, e);
                vwMaster.View.MoveCurrentToFirst();
                IsNew = false;
                ART = "";
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void LOADBYAUF()
        {
            try
            {
                if (!IsSaved)
                    return;
                if (ORDERNUM.Length < 8)
                    return;
                if (ORDERNUM != KEY)
                    return;
                dwData = TBMain.NewRow();
                dwData["KDAMANDANT"] = app.Mandant;
                dwData["KDAKUNDNR"] = " AND KDAKUNDNR='" + ORDERNUM.Substring(0, 8) + "'";
                dwData["KDAADRSL"] = " AND KDAADRSL='" + ORDERNUM.Substring(8, 8) + "'";
                SResult = TMV.QueryKUNDADR(dwData, 1);
                DataTable tb = TBMain.Clone();
                tb.Merge(SResult.Tables[0]);
                foreach (DataRow row in tb.Rows)
                {
                    dwMaster.ItemArray = row.ItemArray;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}